package br.com.vti.sisp_sicrim.antigo.model;

import java.io.Serializable;
import java.sql.ResultSet;
import java.sql.SQLException;

import br.com.vti.sisp_sicrim.antigo.dao.SicControleImpDAO;
import br.com.vti.sisp_sicrim.antigo.util.ExceptionProgram;

@SuppressWarnings("serial")
public class SicControleImp implements Serializable {

	private String id = "";
	private String campoA = "";
	private String campoB = "";
	private String campoC = "";
	private String campoD = "";
	private ResultSet consulta = null;
	private SicControleImpDAO sicControleImpDAO = new SicControleImpDAO();

	public SicControleImp() {
		super();
	}

	public SicControleImp(String id, String campoA, String campoB, String campoC, String campoD) {
		super();
		this.id = id;
		this.campoA = campoA;
		this.campoB = campoB;
		this.campoC = campoC;
		this.campoD = campoD;
	}

	public void grava() {

		try {
			this.id = sicControleImpDAO.insert(campoA, campoB, campoC, campoD);
		} catch (Exception e) {
			throw new ExceptionProgram(e, null, "");
		}
	}

	public void exclui() {

		try {
			sicControleImpDAO.delete(this.id);
		} catch (Exception e) {
			throw new ExceptionProgram(e, null, "");
		}
	}

	public boolean leitura() {

		try {
			if (consulta == null) {
				consulta = sicControleImpDAO.read();
			} else {
				consulta = sicControleImpDAO.pesquisaProximo();
			}
			if (consulta == null) {
				fecha();
				return false;
			}
			formataAtributos(consulta);
		} catch (Exception e) {
			throw new ExceptionProgram(e, null, "");
		}
		return true;
	}

	public boolean fimDados() {
		return !(sicControleImpDAO.temProximo());
	}

	public void salvarQuebras() {

	}

	public void formataAtributos(ResultSet lista) {

		if (lista == null)
			return;
		try {
			this.id = lista.getString("id");
			this.campoA = lista.getString("campoA");
			this.campoB = lista.getString("campoB");
			this.campoC = lista.getString("campoC");
			this.campoD = lista.getString("campoD");
		} catch (SQLException e) {
		}
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getCampoA() {
		return campoA;
	}

	public void setCampoA(String campoA) {
		this.campoA = campoA;
	}

	public String getCampoB() {
		return campoB;
	}

	public void setCampoB(String campoB) {
		this.campoB = campoB;
	}

	public String getCampoC() {
		return campoC;
	}

	public void setCampoC(String campoC) {
		this.campoC = campoC;
	}

	public String getCampoD() {
		return campoD;
	}

	public void setCampoD(String campoD) {
		this.campoD = campoD;
	}

	public void fecha() {
		sicControleImpDAO.fecha();
		consulta = null;
	}

	public long registrosProcessados() {
		return sicControleImpDAO.nroRegistro;
	}

	public long totalRegistros() {
		return sicControleImpDAO.qtdRegistros;
	}

}
